package com.xmut.mall.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmut.mall.store.dao.StoreRegionDao;
import com.xmut.mall.store.entity.StoreRegionEntity;
import com.xmut.mall.store.service.StoreRegionService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;

@Service("storeRegionService")
public class StoreRegionServiceImpl extends ServiceImpl<StoreRegionDao, StoreRegionEntity> implements StoreRegionService {

    /**
     * 初始化地区选择器
     * @return
     */
    @Cacheable(value = "Region",key = "#root.methodName")
    @Override
    public List<StoreRegionEntity> initRegion() {
        List<StoreRegionEntity> regions = this.list(new QueryWrapper<StoreRegionEntity>().eq("parent_id", 0L));
        // 排序
        regions.sort(Comparator.comparing(StoreRegionEntity::getOrderNum));
        return regions;
    }

    /**
     * 根据父Id获取地区信息
     * @param parentId
     * @return
     */
    @Cacheable(value = "Region",key = "#root.methodName+#root.args[0]")
    @Override
    public List<StoreRegionEntity> getRegionByParentId(Long parentId) {
        List<StoreRegionEntity> regions = this.list(new QueryWrapper<StoreRegionEntity>().eq("parent_id", parentId));
        // 排序
        regions.sort(Comparator.comparing(StoreRegionEntity::getOrderNum));
        return regions;
    }
}
